package com.zhang.controller;


import com.zhang.pojo.InputCarBean;
import com.zhang.pojo.UserBean;
import com.zhang.service.IInputCarService;
import com.zhang.service.IInputInfoService;
import com.zhang.service.IProductService;
import com.zhang.util.ResultImg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Api(tags = "采购入库购物车控制接口")
@RestController
@RequestMapping("inputCar")
public class InputCarController {
    @Autowired
    private IInputCarService inputCarService;
    @Autowired
    private IInputInfoService inputInfoService;
    @Autowired
    private IProductService productService;

    @ApiOperation("根据供应商和采购人的id查询入库购物车")
    @GetMapping("findBySupplierAndUser/{supplierId}")
    public List<InputCarBean> findBySupplierAndUser(@PathVariable("supplierId") int supplierId,
                                                    HttpServletRequest req){
        UserBean user = (UserBean) req.getSession().getAttribute("user");
        return inputCarService.findBySupplierAndUser(supplierId,user.getId());
    }

    @ApiOperation("添加购物车商品")
    @PostMapping("add")
    public String add(InputCarBean inputCarBean,HttpServletRequest req){
        UserBean user = (UserBean) req.getSession().getAttribute("user");
        if (user != null){
            inputCarBean.setUserId(user.getId());
        }
        try {
            inputCarService.add(inputCarBean);
            return "ok";
        } catch (Exception e) {
            return "no";
        }
    }

    @ApiOperation("根据商品id删除商品")
    @DeleteMapping("del/{id}")
    public String del(@PathVariable("id") int id){
        try {
            inputCarService.del(id);
            return "ok";
        } catch (Exception e) {
           return "no";
        }
    }

    @ApiOperation("根据商品id修改采购数量")
    @PutMapping("/update")
   public String update(int inputCarId,int number){
        try {
            inputCarService.update(inputCarId, number);
            return "ok";
        } catch (Exception e) {
            return "no";
        }
    }


    @ApiOperation("点击入库,删除购物车.添加入库信息和入库订单。同时添加库存")
    @DeleteMapping("delAll/{supplierId}/{userId}")
    public String delAll(@PathVariable("supplierId") int supplierId,
                       @PathVariable("userId")int userId){
        try {
            inputCarService.delAll(supplierId, userId);
            return "入库成功";
        } catch (Exception e) {
            return "入库失败";
        }
    }


    @ApiOperation("上传图片")
    @RequestMapping("upload")
    public ResultImg upload(@RequestParam("imgFile") MultipartFile imgFile) {
        String filename = imgFile.getOriginalFilename();
        String imgUrl = System.currentTimeMillis() +  filename.substring(filename.lastIndexOf("."));

        File file = new File("d:/img/"+imgUrl);
        ResultImg resultImg = new ResultImg();
        try {
            imgFile.transferTo(file);
            resultImg.setCode("0");
            resultImg.setImgUrl(imgUrl);
        } catch (IOException e) {
            resultImg.setCode("1");
            resultImg.setImgUrl("上传失败");
        }
        return resultImg;
    }
}
